/**
 * @desc 寻找指定组件实例
 * @params {String} type 向上查找还是向下查找
 * @params {Object} context 当前上下文（一般指this，把this 传进来就可以了）
 * @params {String} componentName 要寻找的指定的组件名
 */
export function findComponents(type, context, componentName) {
  if (['$parent', '$children'].indexOf(type) < 0) return

  let currentComponent = context[type]
  if (type === '$parent') currentComponent = [currentComponent]
  let designatedCom = null

  if (currentComponent.length) {
    for (const com of currentComponent) {
      const name = com.$options.name

      if (name === componentName) {
        designatedCom = com
        break
      } else {
        designatedCom = findComponents(type, com, componentName)
        if (designatedCom) break
      }
    }
  }
  return designatedCom
}

/**
 * @desc 格式化日期
 * @params {String} type 根据不同的时间段返回不同的文本
 */
export function changeTime(time) {
  const nowday = Number(new Date().getDate())
  const oldTime = Number(time.split(' ')[0].split('-')[2])
  const nowMonth = Number(new Date().getMonth()+1)
  const oldMonth = Number(time.split(' ')[0].split('-')[1])
  const nowYear = Number(new Date().getFullYear())
  const oldYear = Number(time.split(' ')[0].split('-')[0])
  let nowTime = null;
  if(nowMonth == oldMonth && nowYear == oldYear){
    if ((nowday - oldTime) === 0) {
      nowTime = time.split(' ')[1]
    } else if ((nowday - oldTime) === 1) {
      nowTime = (nowday - oldTime) + '天前'
    } else if ((nowday - oldTime) > 2) {
      nowTime = time.split(' ')[0]
    }else {
      nowTime = time.split(' ')[0]
    }
  }else {
    nowTime = time.split(' ')[0]
  }
  return nowTime
}

/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result

  const later = function() {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp

    // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }

  return function(...args) {
    context = this
    timestamp = +new Date()
    const callNow = immediate && !timeout
    // 如果延时不存在，重新设定延时
    if (!timeout) timeout = setTimeout(later, wait)
    if (callNow) {
      result = func.apply(context, args)
      context = args = null
    }

    return result
  }
}

/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source
 * @returns {Object}
 */
export function deepClone(source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}
